package com.huilai.platform.country_platform.service;

import com.huilai.platform.country_platform.core.Msg;
import com.huilai.platform.country_platform.model.*;
import com.huilai.platform.country_platform.repository.AttractionsBuildingReposity;
import com.huilai.platform.country_platform.repository.AttractionsFoodReposity;
import com.huilai.platform.country_platform.repository.AttractionsWaterscapeReposity;
import com.huilai.platform.country_platform.repository.InformationRecruitmentReposity;
import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.persistence.Query;
import javax.persistence.criteria.Predicate;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 作者：洪霓
 * 日期：2023/2/16
 * 注释：
 */
@Service
public class AttractionsService {

    @Autowired
    private AttractionsBuildingReposity attractionsBuildingReposity;

    @Autowired
    private AttractionsWaterscapeReposity attractionsWaterscapeReposity;

    @Autowired
    private AttractionsFoodReposity attractionsFoodReposity;

    @Autowired
    private InformationRecruitmentReposity informationRecruitmentReposity;

    public Page<AttractionsBuilding> buildingListUI(String searchName, Pageable pageable){
//        查询所有乡镇建筑
        Page<AttractionsBuilding> buildingPage = attractionsBuildingReposity.findAll((root, query, cb) ->{
                    List<Predicate> predicates = new ArrayList<>();
                    //模糊查询
                    if (!StringUtils.isEmpty(searchName)) {
                        predicates.add((cb.like(root.get("name"), "%" + searchName + "%")));
                    }
                    query.where(predicates.toArray(new Predicate[]{}));
                    query.orderBy(cb.desc(root.get("updateTime")));
                    return null;
                }
                ,pageable);
        return buildingPage;
    }

    public Integer addBuilding(AttractionsBuilding attractionsBuilding){
//        userRepository.save(user);
        if (attractionsBuilding.getImage().equals("example")){
            attractionsBuilding.setImage(null);
        }
        Integer integer = attractionsBuildingReposity.addBuilding(attractionsBuilding);
        return integer;
    }

    public AttractionsBuilding getBuildingById (Integer id){
        AttractionsBuilding attractionsBuilding = attractionsBuildingReposity.getOne(id);
        return attractionsBuilding;
    }

    public Integer updateBuilding(AttractionsBuilding attractionsBuilding){
        if (attractionsBuilding.getImage().equals("example")){
            attractionsBuilding.setImage(null);
        }
        Integer integer = attractionsBuildingReposity.updateBuilding(attractionsBuilding);
        return integer;
    }

    public Integer deleteBuildingById(Integer id){
        Integer integer = attractionsBuildingReposity.deleteBuildingById(id);
        return integer;
    }

    public Integer addWaterscape(AttractionsWaterscape attractionsWaterscape){
//        userRepository.save(user);
        if (attractionsWaterscape.getImage().equals("example")){
            attractionsWaterscape.setImage(null);
        }
        Integer integer = attractionsWaterscapeReposity.addWaterscape(attractionsWaterscape);
        return integer;
    }

    public AttractionsWaterscape getWaterscapeById (Integer id){
        AttractionsWaterscape attractionsWaterscape = attractionsWaterscapeReposity.getOne(id);
        return attractionsWaterscape;
    }

    public Integer updateWaterscape(AttractionsWaterscape attractionsWaterscape){
        if (attractionsWaterscape.getImage().equals("example")){
            attractionsWaterscape.setImage(null);
        }
        Integer integer = attractionsWaterscapeReposity.updateWaterscape(attractionsWaterscape);
        return integer;
    }

    public Integer deleteWaterscapeById(Integer id){
        Integer integer = attractionsWaterscapeReposity.deleteWaterscapeById(id);
        return integer;
    }

    public List<AttractionsBuilding>getTopBuilding(){
//        查询前10的乡镇建筑，放置在推荐栏
//        pageable封装了分页参数，（当前页、每页显示的条数），当前页从0开始
        PageRequest pageable = PageRequest.of(0, 10);
        Page<AttractionsBuilding> buildingPage = attractionsBuildingReposity.findAll((root, query, cb) ->{
                    List<Predicate> predicates = new ArrayList<>();
                    query.where(predicates.toArray(new Predicate[]{}));
                    query.orderBy(cb.desc(root.get("updateTime")));
                    return null;
                }
                ,pageable);
        return buildingPage.getContent();
    }

    public Page<AttractionsWaterscape> waterscapeListUI(String searchName, Pageable pageable){
//        查询所有海景
        Page<AttractionsWaterscape>waterscapePage = attractionsWaterscapeReposity.findAll((root, query, cb) ->{
            List<Predicate> predicates = new ArrayList<>();
            //模糊查询
            if (!StringUtils.isEmpty(searchName)) {
                predicates.add((cb.like(root.get("name"), "%" + searchName + "%")));
            }
            query.where(predicates.toArray(new Predicate[]{}));
            query.orderBy(cb.desc(root.get("updateTime")));
            return null;
        },pageable);
        return waterscapePage;
    }


    public List<AttractionsWaterscape>getTopWaterscape(){
//        查询前10的海景，放置在推荐栏
//        pageable封装了分页参数，（当前页、每页显示的条数），当前页从0开始
        PageRequest pageable = PageRequest.of(0, 10);
        Page<AttractionsWaterscape> waterscapePage = attractionsWaterscapeReposity.findAll((root, query, cb) ->{
            List<Predicate> predicates = new ArrayList<>();
            query.where(predicates.toArray(new Predicate[]{}));
            query.orderBy(cb.desc(root.get("updateTime")));
            return null;
        }
        ,pageable);
        return waterscapePage.getContent();
    }


    public AttractionsBuilding findBuildingsById(Integer id){
        return attractionsBuildingReposity.findById(id).orElseThrow(() -> new ServiceException("乡镇建筑id错误!"));
    }

    public AttractionsWaterscape findWaterscapesById(Integer id){
        return attractionsWaterscapeReposity.findById(id).orElseThrow(() -> new ServiceException("海景id错误!"));
    }


    public List<AttractionsFood>getTopFood(){
//        查询前10的美食，放置在推荐栏
//        pageable封装了分页参数，（当前页、每页显示的条数），当前页从0开始
        PageRequest pageable = PageRequest.of(0, 4);
        Page<AttractionsFood> attractionsFoods = attractionsFoodReposity.findAll((root, query, cb) ->{
                    List<Predicate> predicates = new ArrayList<>();
                    query.where(predicates.toArray(new Predicate[]{}));
                    query.orderBy(cb.desc(root.get("updateTime")));
                    return null;
                }
                ,pageable);
        return attractionsFoods.getContent();
    }


}
